﻿(function ($) {
    $.fn.pager = function (settings) {
        var defaults = {
            type: 'number',
            totalPage: 1,
            pageindex: 1,
            nameFunc: 'getData',
            templateNumber: '<a href="javascript:void(0)" onclick="{nameFunc}({page})">{page}</a>',
            templateSelectNumber: '<a href="javascript:void(0)" class="select">{page}</a>',
            template3dot: "<a href='#' class='none'>...</a>",
            templateNext: "<a href='javascript:void(0)' onclick='{nameFunc}({pageNext})'> ></a>",
            templateDisNext: "<a href='javascript:void(0)' class='none'> ></a>",
            templatePre: "<a href='javascript:void(0)' onclick='{nameFunc}({pagePrev})'>< </a>",
            templateDisPre: "<a href='javascript:void(0)' class='none'>< </a>"
        };

        var settings = $.extend(defaults, settings);

        return this.each(function () {
            obj = $(this);
            if (settings.type == 'number') {
                var strPaging = "";                
                if (settings.totalPage < 10) {
                    for (var i = 1; i <= settings.totalPage; i++) {
                        if (i == settings.pageindex)
                            strPaging += settings.templateSelectNumber.replace(/{page}/g, i);
                        else
                            strPaging += settings.templateNumber.replace(/{page}/g, i);
                    }
                }
                else {
                    if (settings.pageindex < 5) {
                        for (var i = 1; i <= 10; i++) {
                            if (i == settings.pageindex)
                                strPaging += settings.templateSelectNumber.replace(/{page}/g, i);
                            else
                                strPaging += settings.templateNumber.replace(/{page}/g, i);
                        }
                        strPaging += settings.template3dot;
                        strPaging += settings.templateNumber.replace(/{page}/g, settings.totalPage - 1);
                        strPaging += settings.templateNumber.replace(/{page}/g, settings.totalPage);                        
                    }
                    else {
                        var temp = 0;                        
                        if (parseInt(settings.totalPage, 10) > (parseInt(settings.pageindex, 10) + 5))
                            temp = parseInt(settings.pageindex, 10) + 5;
                        else
                            temp = parseInt(settings.totalPage, 10);

                        if (parseInt(settings.pageindex, 10) >= 7) {                            
                            strPaging += settings.templateNumber.replace(/{page}/g, 1);
                            strPaging += settings.templateNumber.replace(/{page}/g, 2);
                            strPaging += settings.template3dot;                                                        
                        }
                        //alert(temp);
                        for (var i = (settings.pageindex - 4); i <= temp; i++) {                        
                            if (i == settings.pageindex)
                                strPaging += settings.templateSelectNumber.replace(/{page}/g, i);
                            else
                                strPaging += settings.templateNumber.replace(/{page}/g, i);                                             
                        }
                        if (parseInt(settings.totalPage, 10) > (parseInt(settings.pageindex, 10) + 5)) {                        
                            strPaging += settings.template3dot;
                            strPaging += settings.templateNumber.replace(/{page}/g, settings.totalPage - 1);
                            strPaging += settings.templateNumber.replace(/{page}/g, settings.totalPage);                            
                        }

                    }
                }

                var bla = "";
                if (settings.pageindex == 1) {
                    if (settings.totalPage > 1)
                        bla = settings.templateDisPre + strPaging + settings.templateNext;
                    else
                        bla = settings.templateDisPre + strPaging + settings.templateDisNext;
                }
                else if (settings.pageindex == settings.totalPage)
                    bla = settings.templatePre + strPaging + settings.templateDisNext;
                else
                    bla = settings.templatePre + strPaging + settings.templateNext;

                
                bla = bla.replace(/{nameFunc}/g, settings.nameFunc).replace(/{pagePrev}/g, (parseInt(settings.pageindex, 10) - 1)).replace(/{pageNext}/g, (parseInt(settings.pageindex, 10) + 1));
                $(this).html(bla);
            }
        })
    }

})(jQuery);